﻿using FileTransferProtocol;
using System;
using System.Net;
using System.Net.Sockets;

namespace TestReceiver
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, 8030));
            listener.Start();

            while (true)
            {
                var asy = listener.BeginAcceptSocket(new AsyncCallback(Receive), listener);
                asy.AsyncWaitHandle.WaitOne();
            }
        }

        static void Receive(IAsyncResult result)
        {
            var listener = (TcpListener)result.AsyncState;

            var client = listener.EndAcceptTcpClient(result);

            var filename = string.Empty;
            using (var stream = client.GetStream())
            {
                filename = new FileReceiver().Receive(stream, new FileSaver());
            }

            Console.WriteLine($"接收文件完成 {filename}");
        }
    }
}
